package com.wm.walmart.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wm.walmart.entity.Modules;
import com.wm.walmart.service.IModulesService;
import com.wm.walmart.util.JwtToken;
import com.wm.walmart.util.Token;
import com.wm.walmart.util.TreeData;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xieyize
 * @since 2019-12-10
 */
@RestController
@RequestMapping("/modules/")
public class ModulesController {

	@Resource
	private IModulesService mService;

	/**
	 * 添加模板信息
	 * http://localhost:8080/walmart/modules/insertModules?mName=aa&mParentId=1&mWeight=10
	 */
	@RequestMapping("insertModules")
	public Object insertModules(Modules modules) {
		return mService.save(modules);
	}

	/**
	 * 根据模板id修改该模板信息
	 * http://localhost:8080/walmart/modules/updateModules?mId=5&mName=ab&mParentId=12&mWeight=11
	 */
	@RequestMapping("updateModules")
	public Object updateModules(Modules modules) {
		return mService.updateById(modules);
	}

	/**
	 * 根据模板id删除该模板信息 http://localhost:8080/walmart/modules/deleteModulesById?mId=5
	 */
	@RequestMapping("deleteModulesById")
	public Object deleteModulesById(Integer mId) {
		return mService.removeById(mId);
	}

	/**
	 * 根据角色id获取该用户的模块信息
	 */
	@RequestMapping("getModule")
	public List<Modules> getModuleByUserId(String tokenId) {
		try {
			Token token = JwtToken.unsign(tokenId, Token.class);
			List<Modules> menu = mService.getModuleByUserId(token.getUserId());
			return TreeData.getParent(menu, null);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
